/**
 * MutiChatRoom.java
 */
package net.blogjava.mobile.gtalk;

import java.util.Collection;
import java.util.Random;

import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.muc.MultiUserChat;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * MutiChatRoom.java
 * 
 * @author Wone 2011-5-11 多人聊天室 SVN Test!!
 */
public class MutiChatRoom extends Activity implements OnMessageListener{

	private final static String TAG = "MutiChatRoom";
	
	private MultiUserChat muc;
	
	private MessageReceiver mUpdateMessage;
	private Thread mThread;
	
	private EditText messageListEdit;
	private EditText messageEdit;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.chatroom);

		LogAt.v(TAG, "onCreate();");

		String multiUserRoom="myroom@conference.wone-pc";
		
		try {
			
			/*
			 * 使用XMPPConnection创建一个MultiUserChat
			 * 注意：第二个参数格式必须后缀为@conference.wone-pc，不能用IP地址代替
			 */ 
			muc = new MultiUserChat(GTalk.mConnection,
					multiUserRoom);

			// 创建聊天室
			muc.join("keepmovingzxw");

			// 发送一个空表单配置这显示我们想要一个instant room
//			muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));

			LogAt.v(TAG, "创建聊天室!");

//			 获得RosterEntry对象的集合，每一个RosterEntry对象表示一个联系人
//			Collection<RosterEntry> entries = GTalk.mRoster.getEntries();
//
//			String user = null;
//
//			for (RosterEntry entry : entries) {
//				user = entry.getUser();
//				LogAt.v(TAG, "user=" + user);
//				muc.join(user);
//			}

			LogAt.v(TAG, "多人加入聊天室");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogAt.v(TAG, "Exception:", e);
		}

		//设置消息监听器
		
//		mUpdateMessage = new MessageReceiver(multiUserRoom);
		mUpdateMessage = new MessageReceiver("admin@wone-pc");
		mUpdateMessage.setOnMessageListener(this);
		mThread = new Thread(mUpdateMessage);
		mThread.start();
		

		messageListEdit = (EditText) findViewById(R.id.etMessageList);
		messageEdit = (EditText) findViewById(R.id.etMessage);
		Button btnSend = (Button) findViewById(R.id.btnSend);

		btnSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				String msg = messageEdit.getText().toString();
				if (!"".equals(msg)) {
					
					try {
						LogAt.v(TAG, "发送消息！");
						muc.sendMessage(msg);
						messageEdit.setText("");
						messageListEdit.append("我：\n");
						messageListEdit.append(msg + "\n\n");

					} catch (Exception e) {
						// TODO: handle exception
						LogAt.v(TAG, "Exception:", e);
					}
				}else{
					LogAt.v(TAG, "发送消息为空！");
				}

			}

		});

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		try {

			muc.destroy(null, null);

			mUpdateMessage.flag = false;

			mUpdateMessage.mCollector.cancel();

		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			LogAt.v(TAG, "Exception:", e);
		}

		super.onDestroy();
	}
	
	private void sendMsg(String from, String msg){
		
		LogAt.v(TAG, "sendMsg():from="+from);
		LogAt.v(TAG, "sendMsg():msg="+msg);
		
		messageListEdit.append(GTalk.mUtil.getLeftString(from, "/") + ":\n");
		messageListEdit.append(msg + "\n\n");

	}
	
	/* (non-Javadoc)
	 * <p>Title: processMessage</p> 
	 * <p>Description: </p> 
	 * @param message 
	 * @see net.blogjava.mobile.gtalk.OnMessageListener#processMessage(org.jivesoftware.smack.packet.Message)
	 */
	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		LogAt.v(TAG, "receive message");
		sendMsg(message.getFrom(),message.getBody());
	}
	

}


